﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using KeepItFreeMvc.Models;

namespace KeepItFreeMvc.Controllers
{

        public class UserController : ApiController
        {
            private KeepItFreeMvc.Models.KeepItFreeEntities1 repository = new KeepItFreeMvc.Models.KeepItFreeEntities1();

            public IEnumerable<User> GetAllUsers()
            {
                return repository.User.ToList();
            }

            public User GetUserById(int id)
            {
                var user = repository.User.FirstOrDefault((p) => p.Id == id);
                if (user == null)
                {
                    //var resp = new HttpResponseMessage(HttpStatusCode.NotFound);
                    //throw new HttpResponseException(resp);
                }
                return user;
            }

            public IEnumerable<User> GetUserByName(string name)
            {
                return repository.User.ToList().Where(
                    (p) => string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase));
            }

        }


        /*
        public class UserController : ApiController
        {
            //private readonly KeepItFreeMvc.Models.Entities UserRepository = new KeepItFreeMvc.Models.Entities();
            private KeepItFreeMvc.Models.Entities UserRepository = new KeepItFreeMvc.Models.Entities();

        
            //public UserController(IUserRepository UserRepository)
            //{
            //    this.UserRepository = UserRepository;
            //}
        

            // GET /api/user
            public IEnumerable<User> Get()
            {
                return UserRepository.User.ToList();
            }

            // GET /api/user/5
            public User Get(int id)
            {
                var User = UserRepository.User.ToList().Find(id);
                //if (User == null) throw new HttpResponseException(HttpStatusCode.NotFound);
                return User;
            }

            // POST /api/user
            public HttpResponseMessage<User> Post(User value)
            {
                if (ModelState.IsValid)
                {
                    UserRepository.InsertOrUpdate(value);
                    UserRepository.Save();

                    var response = new HttpResponseMessage<User>(value, HttpStatusCode.Created);

                    string uri = Url.Route(null, new { id = value.Id });
                    response.Headers.Location = new Uri(Request.RequestUri, uri);

                    return response;
                }
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            // PUT /api/user/5
            public HttpResponseMessage<User> Put(int id, User value)
            {
                if (ModelState.IsValid)
                {
                    UserRepository.InsertOrUpdate(value);
                    UserRepository.Save();
                    return new HttpResponseMessage(HttpStatusCode.NoContent);
                }
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            // DELETE /api/user/5
            public void Delete(int id)
            {
                var User = UserRepository.User.Find(id);
                if (User == null)
                    throw new HttpResponseException(HttpStatusCode.NotFound);

                UserRepository.User.Delete(id);
            }

        }
        */

}
